package qeb.handheldmedical.adapter;

import org.json.JSONArray;
import org.json.JSONObject;

import qeb.handheldmedical.R;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PhoneAdapter extends BaseAdapter{

	private static final String TAG = "PhoneAdapter";
	
	//private ImageSwitcher mImageLoader;
	private int mCount;
	private Context mContext;
	private JSONArray phoneJsonArray =null;
	
	public PhoneAdapter(int count, Context context, JSONArray phoneJsonArray) {
		this.mCount = count;
		this.mContext = context;
		this.phoneJsonArray = phoneJsonArray;
		//mImageLoader = new ImageSwitcher(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder = null;
		convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_phone, null);
			viewholder = new ViewHolder();
			viewholder.txt_Deviceid = (TextView)convertView.findViewById(R.id.txt_Deviceid);
			viewholder.txt_Applicantname = (TextView)convertView.findViewById(R.id.txt_Applicantname);
			viewholder.txt_Applicantid = (TextView)convertView.findViewById(R.id.txt_Applicantid);
			viewholder.txt_Applystate = (TextView)convertView.findViewById(R.id.txt_Applystate);
			convertView.setTag(viewholder);
			
			try {
				JSONObject jobj = phoneJsonArray.getJSONObject(position);
				if(jobj!=null)
				{
					viewholder.txt_Deviceid.setText(jobj.getString("Deviceid"));
					viewholder.txt_Applicantname.setText(jobj.getString("Applicantname"));
					viewholder.txt_Applicantid.setText(jobj.getString("Applicantid"));
					viewholder.txt_Applystate.setText(jobj.getString("Applystate"));
					if(jobj.getString("Applystate").equals("1")){
						viewholder.txt_Applystate.setText("已审核");
					}
					else if(jobj.getString("Applystate").equals("-1")){
						viewholder.txt_Applystate.setText("已作废");
						viewholder.txt_Applystate.setTextColor(Color.RED);
					}
					else{
						viewholder.txt_Applystate.setText("未审核");
						viewholder.txt_Applystate.setTextColor(Color.BLUE);
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				Log.e(TAG, e.toString());
			}
		
		return convertView;
	}
	
	static class ViewHolder {
		//ImageView imgPatient;
		TextView txt_Deviceid;
		TextView txt_Applicantname;
		TextView txt_Applicantid;
		TextView txt_Applystate;
	}
}